﻿Imports MindFusion.Diagramming

Public Class ClsDSaModFile
    Inherits ClsFile
    'represents cover for aMod file (file with all versions of DS)

#Region "Attributes"
#Region "Private attributes"
    Private MoDat As amod2.Dat '= New amod2.Dat()
    Private MoOpenedEditableVersions As ClsMMCollection = Nothing 'all opened versions of file
    Private MoOpenedReadOnlyVersions As ClsMMCollectionList = Nothing 'all opened readonly versions of file
    Private MbOpenedUseCaseDiagram As Boolean = False 'if use case diagram is opened
#End Region
#End Region
#Region "Constructors"
    Sub New(ByVal IsModuleName As String, ByVal IsHumanModuleName As String)
        MyBase.New()
        MoDat = New AMod2.Dat(IsModuleName, IsHumanModuleName, "")
        ''DOCASNE - tahanie components - ZDRZIAVACKA STRASNA!!!
        MoDat.LoadDsComponents(frmMainForm.ApplicationPaths.ComponentsXMLpath)
        ''DOCASNE
        'MoDat.CreateNewVersion("verzia2")
        'MoDat.CreateNewVersion("verzia3")
        'MoDat.CreateNewVersion("verzia4")
        ''DOCASNE
    End Sub
    Sub New(ByVal IsModuleName As String, ByVal IsHumanModuleName As String, ByVal IsPath As String)
        MyBase.New(IsPath)
        MoDat = New AMod2.Dat(IsModuleName, IsHumanModuleName, "")
    End Sub
#End Region
#Region "Properties"
#Region "Public attributes"
    'Dat property for export to xMod
    Public ReadOnly Property Dat() As amod2.Dat
        Get
            Return MoDat
        End Get
    End Property
    Public ReadOnly Property DSaModAllVersionsDS() As amod2.DsAMod
        Get
            Return MoDat.DsAModAll
        End Get
    End Property
    'directory with aMOd file
    Public ReadOnly Property aModFileDirectory() As String
        Get
            Return IO.Path.GetDirectoryName(MsPath)
        End Get
    End Property
    Public ReadOnly Property OpenedEditableVersions() As ClsMMCollection
        Get
            Return MoOpenedEditableVersions
        End Get
    End Property
    Public ReadOnly Property OpenedReadOnlyVersions() As ClsMMCollectionList
        Get
            Return MoOpenedReadOnlyVersions
        End Get
    End Property
    Public ReadOnly Property OpenedUseCaseDiagram() As Boolean
        Get
            Return MbOpenedUseCaseDiagram
        End Get
    End Property
#End Region
#End Region
#Region "Functions"
#Region "Open file&DS, Save file&DS, Close file&DS"
    'opens DS aMod dataset
    Private Function OpenDSaModDataset() As GeActionResult
        Try
            Dim LsString As String = ""
            If MeStateOfFile = GeFileState.kkReadyForOpen Then
                If MoDat.LoadAmod(MsPath, LsString) Then
                    Return GeActionResult.kkOk
                Else
                    Throw New Exception
                End If
            Else
                Throw New Exception
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'new file
    Private Function CreateNewFile() As ClsDsAModOneVersion
        Try
            If MeStateOfFile = GeFileState.kkNotInit Then
                MeStateOfFile = GeFileState.kkNewFile
                MoOpenedEditableVersions = New ClsMMCollection
                MoOpenedReadOnlyVersions = New ClsMMCollectionList

                Return OpenVersion(MoDat.GetLastVersion, False)
            Else
                Throw New Exception
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return Nothing
        End Try
    End Function
    'inits after new file
    Public Function InitAfterNewFile(ByVal IsPath As String) As GeActionResult
        Try
            If MeStateOfFile = GeFileState.kkNewFile Then
                MeStateOfFile = GeFileState.kkOpened
                MsPath = IsPath
                Return GeActionResult.kkOk
            Else
                Throw New Exception
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'new physical aMod file with empty DS
    Public Function CreateVirginFile(ByVal IsPath As String) As GeActionResult
        Try
            Dim LeResult As GeActionResult = GeActionResult.kkOk
            Dim LoDsAModOneVersion As ClsDsAModOneVersion = CreateNewFile()

            If LoDsAModOneVersion IsNot Nothing Then
                LeResult = InitAfterNewFile(IsPath)
            Else
                Throw New Exception
            End If

            If LeResult = GeActionResult.kkOk Then
                LeResult = SaveVersion(Dat.GetLastVersion)
            Else
                Throw New Exception
            End If

            If LeResult = GeActionResult.kkOk Then
                LeResult = SaveFile()
            Else
                Throw New Exception
            End If

            If LeResult = GeActionResult.kkOk Then
                LeResult = CloseVersion(LoDsAModOneVersion)
            Else
                Throw New Exception
            End If

            If LeResult = GeActionResult.kkOk Then
                LeResult = CloseFile()
            Else
                Throw New Exception
            End If

            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'open file
    Public Overrides Function OpenFile() As GeActionResult
        Try
            If MyBase.OpenFile = GeActionResult.kkOk Then
                If OpenDSaModDataset() = GeActionResult.kkOk Then
                    MeStateOfFile = GeFileState.kkOpened
                    MoOpenedEditableVersions = New ClsMMCollection
                    MoOpenedReadOnlyVersions = New ClsMMCollectionList
                    Return GeActionResult.kkOk
                Else
                    Throw New Exception
                End If
            Else
                Throw New Exception
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'save file
    Public Function SaveFile() As GeActionResult
        Try
            If MeStateOfFile = GeFileState.kkOpened Then
                Dim LsString As String = ""
                If Not IO.Directory.Exists(aModFileDirectory) Then
                    TryToCreateFolder(aModFileDirectory, False) 'creating direcotry for aMod file
                End If
                If MoDat.SaveAMod(MsPath, LsString) Then
                    Return GeActionResult.kkOk
                Else
                    Throw New Exception
                End If
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'opens version - returns opened version (ClsDsAModOneVersion object)
    Public Function OpenVersion(ByVal IiVID As Integer, ByVal IbOpenedForReading As Boolean) As ClsDsAModOneVersion
        Try
            If MeStateOfFile = GeFileState.kkOpened Or MeStateOfFile = GeFileState.kkNewFile Then
                If Not IbOpenedForReading AndAlso MoOpenedEditableVersions.IsItemInCollection(IiVID) = True Then
                    LogEvent("Verzia """ & IiVID & """ je uz otvorena!")
                    Return Nothing
                Else
                    Dim LoDDsAModOneVersion As ClsDsAModOneVersion = Nothing 'object of aMod version
                    Dim LoDsAModOneVersion As AMod2.DsAModOneVersion = Nothing 'aMod version DS 

                    LoDsAModOneVersion = MoDat.GetVersionDataset(IiVID)
                    If LoDsAModOneVersion IsNot Nothing Then
                        LoDDsAModOneVersion = New ClsDsAModOneVersion()
                        If LoDDsAModOneVersion.OpenVersion(LoDsAModOneVersion, IbOpenedForReading) = GeActionResult.kkOk Then
                            If IbOpenedForReading Then
                                MoOpenedReadOnlyVersions.AddItem(CStr(IiVID))
                            Else
                                MoOpenedEditableVersions.AddItem(LoDDsAModOneVersion, CStr(IiVID))
                            End If
                            Return LoDDsAModOneVersion
                        Else
                            Throw New Exception
                        End If
                    Else
                        Throw New Exception
                    End If
                End If
            Else
                Throw New Exception
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return Nothing
        End Try
    End Function
    'saves version
    Public Function SaveVersion(ByVal IiVID As Integer) As GeActionResult
        Try
            If MeStateOfFile = GeFileState.kkOpened Then
                Dim LoDDsAModOneVersion As ClsDsAModOneVersion = Nothing 'object of aMod version
                If Not MoOpenedEditableVersions.IsItemInCollection(CStr(IiVID)) Then
                    MessageBox.Show("Verziu " & (IiVID) & " nie je mozne ulozit! Bud nie je otovrena alebo je urcena iba na citanie.", "Chyba pri ukladanie verzie", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Return GeActionResult.kkError
                Else
                    LoDDsAModOneVersion = MoOpenedEditableVersions.GetItem(CStr(IiVID))
                    If LoDDsAModOneVersion IsNot Nothing Then
                        'DOCASNE - InsertOneVersionToAMod urobit ako metodu, aby vratila navrat. hodnotu
                        MoDat.InsertOneVersionToAMod(LoDDsAModOneVersion.DSaModVersion)
                        LoDDsAModOneVersion.WasModified = False
                        Return GeActionResult.kkOk
                    Else
                        Throw New Exception
                    End If
                End If
            Else
                Throw New Exception
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'closes version
    Public Function CloseVersion(ByVal IoDsAModOneVersion As ClsDsAModOneVersion) As GeActionResult
        Try
            If IoDsAModOneVersion.OpenedForReading Then
                Return MoOpenedReadOnlyVersions.RemoveItemByValue(IoDsAModOneVersion.GetVersionVID)
            Else
                Return MoOpenedEditableVersions.RemoveItem(IoDsAModOneVersion.GetVersionVID)
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'creates new version
    Public Function CreateNewVersion(ByVal IsModuleName As String, ByVal IsHumanModuleName As String) As GeActionResult
        Try
            MoDat.CreateNewVersion(IsModuleName, IsHumanModuleName, "")
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'closes file
    Public Overrides Function CloseFile() As GeActionResult
        Try
            If MyBase.CloseFile = GeActionResult.kkOk Then
                If MoOpenedEditableVersions.IsEmpty AndAlso MoOpenedReadOnlyVersions.IsEmpty Then
                    MeStateOfFile = GeFileState.kkNotInit
                    MsPath = ""
                    MoOpenedEditableVersions = Nothing
                    MoOpenedReadOnlyVersions = Nothing
                    Return GeActionResult.kkOk
                Else
                    Throw New Exception("Subor nie je mozne zavriet! Je otvorena jeho jedna alebo viac verzii.")
                End If
            Else
                Throw New Exception
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'opens use case diagram - this set MbOpenedUseCaseDiagram variable which is used while checking for close of aMod file
    Public Function OpenUseCaseDiagram() As GeActionResult
        Try
            Dim LeResult As GeActionResult = GeActionResult.kkOk
            If Not MbOpenedUseCaseDiagram Then
                MbOpenedUseCaseDiagram = True
                LeResult = GeActionResult.kkOk
            Else
                MessageBox.Show("Use case diagram je uz otvoreny!", "Uz otvoreny use case diagrsm", MessageBoxButtons.OK, MessageBoxIcon.Error)
                LeResult = GeActionResult.kkError
            End If

            Return LeResult
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'closes use case diagram - this set MbOpenedUseCaseDiagram variable which is used while checking for close of aMod file
    Public Function CloseUseCaseDiagram() As GeActionResult
        Try
            Dim LeResult As GeActionResult = GeActionResult.kkOk
            If MbOpenedUseCaseDiagram Then
                MbOpenedUseCaseDiagram = False
                LeResult = GeActionResult.kkOk
            Else
                MessageBox.Show("Use case diagram nie je mozne zavret, pretoze nie je otvoreny!", "Neotvoreny use case diagrsm", MessageBoxButtons.OK, MessageBoxIcon.Error)
                LeResult = GeActionResult.kkError
            End If

            Return LeResult
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "Manipulating with DS"
#Region "INSERT"
    Public Function InsertDuplicateVersion(ByVal IiVersion As Integer) As GeActionResult
        Try
            Dat.DuplicateVersion(IiVersion)

            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "SELECT"
    'gets all rows from Versions
    Public Function GetVersionRowsAll() As DataRow()
        Try
            Return MoDat.DsAModAll.VersionsTable.Select()

        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return Nothing
        End Try
    End Function
    'gets row from Versions
    Public Function GetVersionRow(ByVal IiVID As Integer) As DataRow
        Try
            Dim LoRows As DataRow()
            LoRows = MoDat.DsAModAll.VersionsTable.Select(String.Format("{0}='{1}'", AMod2.DsAMod.TableSysnames.Versions.Columns.VID, IiVID))
            If LoRows IsNot Nothing AndAlso LoRows.Length = 1 Then
                Return LoRows(0)
            Else
                Throw New Exception
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return Nothing
        End Try
    End Function
    'gets all rows from Versions
    Public Function GetAllVersionRows() As DataRow()
        Try
            Return MoDat.DsAModAll.VersionsTable.Select()

        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return Nothing
        End Try
    End Function
    'gets all version numbers
    Public Function GetAllVersionNumbers() As Collection
        Try
            Dim LoRows As DataRow() = GetAllVersionRows()
            Dim LoCollectionOfVersions As Collection = New Collection

            For Each LoRow As DataRow In LoRows
                LoCollectionOfVersions.Add(LoRow(AMod2.DsAMod.TableSysnames.Versions.Columns.VID))
            Next

            Return LoCollectionOfVersions
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return Nothing
        End Try
    End Function
    'gets last version module name = actual name of module
    Public Function GetLastVersionModuleName() As String
        Try
            Dim LoRow As DataRow
            LoRow = GetVersionRow(MoDat.GetLastVersion)

            If LoRow IsNot Nothing Then
                Return LoRow(AMod2.DsAMod.TableSysnames.Versions.Columns.ModulName)
            Else
                Throw New Exception
            End If

            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return ""
        End Try
    End Function
    'gets last version human module name = actual human name of module
    Public Function GetLastVersionHumanModuleName() As String
        Try
            Dim LoRow As DataRow
            LoRow = GetVersionRow(MoDat.GetLastVersion)

            If LoRow IsNot Nothing Then
                Return LoRow(AMod2.DsAMod.TableSysnames.Versions.Columns.HumanModulName)
            Else
                Throw New Exception
            End If

            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return ""
        End Try
    End Function
    'gets version name
    Public Function GetVersionName(ByVal IiVersion As Integer) As String
        Try
            Dim LoRow As DataRow
            LoRow = GetVersionRow(IiVersion)

            If LoRow IsNot Nothing Then
                Return LoRow(AMod2.DsAMod.TableSysnames.Versions.Columns.VersionName)
            Else
                Throw New Exception
            End If

            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return ""
        End Try
    End Function
    'gets RTF description of version
    Public Function GetRTFdescriptionOfVersion(ByVal IiVID As Integer) As String
        Try
            Dim LoRow As DataRow = GetVersionRow(IiVID)
            Return LoRow(AMod2.DsAMod.TableSysnames.Versions.Columns.RTFDescription)

        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return ""
        End Try
    End Function
    'gets use case diagram data
    Public Function GetUseCaseDiagramData() As String
        Try
            Dim LoRows As DataRow()
            LoRows = MoDat.DsAModAll.Diagrams.Select

            If LoRows.Length = 1 Then
                Return LoRows(0)(AMod2.DsAMod.TableSysnames.Diagrams.Columns.UseCase)
            Else
                Return ""
            End If

        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return ""
        End Try
    End Function
#End Region
#Region "UPDATE"
    'update use case diagram data
    Public Function UpdateUseCaseDiagramData(ByVal IsDiagramData As String) As GeActionResult
        Try
            Dim LoRows As DataRow()
            LoRows = MoDat.DsAModAll.Diagrams.Select

            If LoRows.Length = 1 Then
                LoRows(0)("UseCase") = IsDiagramData
            Else
                Dim LoRow As DataRow = MoDat.DsAModAll.Diagrams.NewRow
                LoRow("UseCase") = IsDiagramData

                MoDat.DsAModAll.Diagrams.Rows.Add(LoRow)
            End If
            MoDat.DsAModAll.AcceptChanges()

            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update human module names of all versions
    Public Function UpdateHumanModuleNamesOfAllVersions(ByVal IsNewHumanModuleName As String) As GeActionResult
        Try
            For Each LoRow In MoDat.DsAModAll.VersionsTable.Rows
                LoRow(AMod2.DsAMod.TableSysnames.Versions.Columns.HumanModulName) = IsNewHumanModuleName
            Next

            'also saving of aMod file coz of no "diskette icon" evidence of this change                      
            SaveFile()

            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update module names of all versions. Also change aMod file path
    Public Function UpdateModuleNamesOfAllVersions(ByVal IsNewModuleName As String, ByVal IsNewAmodFilePath As String) As GeActionResult
        Try
            For Each LoRow In MoDat.DsAModAll.VersionsTable.Rows
                LoRow(AMod2.DsAMod.TableSysnames.Versions.Columns.ModulName) = IsNewModuleName
            Next
            SetNewFilePath(IsNewAmodFilePath)

            'also saving of aMod file coz of no "diskette icon" evidence of this change                      
            SaveFile()

            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update version name
    Public Function UpdateVersionName(ByVal IiVersion As Integer, ByVal IsNewVersionName As String) As GeActionResult
        Try
            Dim LoRow As DataRow
            LoRow = GetVersionRow(IiVersion)

            If LoRow IsNot Nothing Then
                LoRow(AMod2.DsAMod.TableSysnames.Versions.Columns.VersionName) = IsNewVersionName
            Else
                Throw New Exception
            End If

            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update RTF description of version 
    Public Function UpdateRTFDescriptionOfVersion(ByVal IiVID As Integer, ByVal IsRTFtext As String) As GeActionResult
        Try
            Dim LoRow As DataRow = GetVersionRow(IiVID)
            LoRow(AMod2.DsAMod.TableSysnames.Versions.Columns.RTFDescription) = IsRTFtext

            'also saving of aMod file coz VersionModuleName was changed in aMod file DS and not in DsAModOneVersion DS, so theres no "diskette icon" evidence of this change                      
            SaveFile()

            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "DELETE"
    'deletes version from aMod DS
    Public Function DeleteVersion(ByVal IiVID As Integer)
        Try
            Dim LoRow As DataRow = GetVersionRow(IiVID)
            LoRow.Delete()
            'also saving of aMod file coz VersionModuleName was changed in aMod file DS and not in DsAModOneVersion DS, so theres no "diskette icon" evidence of this change                      
            SaveFile()

            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#End Region
#Region "Support functions"
    'gets MM collection of opened versions
    Public Function GetOpenedVersions() As ClsMMCollection
        Return MoOpenedEditableVersions
    End Function
#End Region
#End Region

End Class

